export const myMixin = {
  // data() {
  //   return {
  //     mixinData: 'This is mixin data'
  //   };
  // },
  methods: {
    goto(to) {
      this.$router.push(to);
    },
    successMessage(msg) {
      this.$message({
        message: msg,
        type: "success",
      });
    },
    errorMessage(msg) {
      this.$message({
        message: msg,
        type: "error",
      });
    },
    calculateRow(row) {
      // 使用正则表达式验证输入值是否为有效的正整数
      const positiveIntegerRegex = /^\d+$/; // 匹配正整数
      const discountTaxRateRegex = /^(?:[1-9]?\d{1,2}|100)(?:\.\d{1,2})?$/;
      // 验证和重置数量
      if (
        !positiveIntegerRegex.test(row.totalNum) ||
        row.totalNum === "" ||
        row.totalNum == 0
      ) {
        this.$message({
          showClose: true,
          message: "数量必须是正整数",
          type: "warning",
        });

        // 将字符串转换为浮点数
        let discountValue = parseFloat(row.totalNum);

        // 如果isNaN，则设置为0
        if (isNaN(discountValue)) {
          row.totalNum = 1;
        } else {
          // 修正为0-100之间的值
          if (discountValue <= 0) {
            row.totalNum = 1;
          } else {
            // 四舍五入到两位小数
            row.totalNum = parseFloat(discountValue.toFixed(0));
          }
        }
      }

      // 验证和重置购货单价（假设购货单价可以是浮点数）
      const positiveNumberRegex = /^\d+(\.\d{1,2})?$/; // 匹配正数，小数点最多两位
      if (
        !positiveNumberRegex.test(row.purchasePrice) ||
        row.purchasePrice === ""
      ) {
        this.$message({
          showClose: true,
          message: "购货单价必须是正数,且小数点后最多两位",
          type: "warning",
        });
        // 将字符串转换为浮点数
        let discountValue = parseFloat(row.purchasePrice);
        // 如果isNaN，则设置为0
        if (isNaN(discountValue)) {
          row.purchasePrice = 0;
        } else {
          // 修正为0-100之间的值
          if (discountValue < 0) {
            row.purchasePrice = 0;
          } else {
            // 四舍五入到两位小数
            row.purchasePrice = parseFloat(discountValue.toFixed(2));
          }
        }
      }

      // 验证和重置折扣率和税率
      if (!discountTaxRateRegex.test(row.discount) || row.discount === "") {
        this.$message({
          showClose: true,
          message: "折扣率必须是0-100之间小数点最多两位的数",
          type: "warning",
        });

        // 将字符串转换为浮点数
        let discountValue = parseFloat(row.discount);

        // 如果isNaN，则设置为0
        if (isNaN(discountValue)) {
          row.discount = 0;
        } else {
          // 修正为0-100之间的值
          if (discountValue < 0) {
            row.discount = 0;
          } else if (discountValue > 100) {
            row.discount = 100;
          } else {
            // 四舍五入到两位小数
            row.discount = parseFloat(discountValue.toFixed(2));
          }
        }
      }
      if (!discountTaxRateRegex.test(row.taxRate) || row.taxRate === "") {
        this.$message({
          showClose: true,
          message: "税率必须是0-100之间小数点最多两位的数",
          type: "warning",
        });

        // 将字符串转换为浮点数
        let discountValue = parseFloat(row.taxRate);

        // 如果isNaN，则设置为0
        if (isNaN(discountValue)) {
          row.taxRate = 0;
        } else {
          // 修正为0-100之间的值
          if (discountValue < 0) {
            row.taxRate = 0;
          } else if (discountValue > 100) {
            row.taxRate = 100;
          } else {
            // 四舍五入到两位小数
            row.taxRate = parseFloat(discountValue.toFixed(2));
          }
        }
      }

      // 将字符串转换为浮点数
      row.totalNum = parseInt(row.totalNum, 10);
      row.purchasePrice = parseFloat(row.purchasePrice);
      row.discount = parseFloat(row.discount);
      row.taxRate = parseFloat(row.taxRate);

      // 计算折扣额
      row.discountAmount = (
        row.totalNum *
        row.purchasePrice *
        (row.discount / 100)
      ).toFixed(2);

      // 计算含税单价
      row.totalPrice = (row.purchasePrice * (1 + row.taxRate / 100)).toFixed(2);

      // 计算折扣（折扣必须小于10）
      row.discountNoRate = (10 - row.discount / 10).toFixed(2);

      // 计算金额
      row.amount = (
        row.totalNum *
        row.purchasePrice *
        (1 - row.discount / 100)
      ).toFixed(2);

      // 计算税额
      row.tax = (row.amount * (row.taxRate / 100)).toFixed(2);

      // 计算价税合计
      row.totalMoney = (parseFloat(row.amount) + parseFloat(row.tax)).toFixed(
        2
      );
    },
    calculateDiscountNoRate(row) {
      const decimalRegex = /^(?:[0-9]+(?:\.[0-9]{1,2})?|10(?:\.0+)?)$/;

      // 验证和重置数量
      if (!decimalRegex.test(row.discountNoRate) || row.discountNoRate === "") {
        this.$message({
          showClose: true,
          message: "改为0-10的小数点最多后两位的数字",
          type: "warning",
        });

        // 将字符串转换为浮点数
        let discountValue = parseFloat(row.discountNoRate);

        // 如果isNaN，则设置为0
        if (isNaN(discountValue)) {
          row.discountNoRate = 0;
        } else {
          // 修正为0-100之间的值
          if (discountValue < 0) {
            row.discountNoRate = 0;
          } else if (discountValue > 10) {
            row.discountNoRate = 10;
          } else {
            // 四舍五入到两位小数
            row.discountNoRate = parseFloat(discountValue.toFixed(2));
          }
        }
      }

      // 将字符串转换为浮点数
      row.discountNoRate = parseFloat(row.discountNoRate);

      row.discount = ((10 - row.discountNoRate) * 10).toFixed(2);

      this.calculateRow(row);
    },
    calculateRowTotalMoney(row) {
      //价税合计
      // 使用正则表达式验证输入值是否为有效的正整数
      const positiveNumberRegex = /^\d+(\.\d{1,2})?$/; // 匹配正数，小数点最多两位
      // 验证和重置价税合计（假设价税合计可以是浮点数）
      if (!positiveNumberRegex.test(row.totalMoney) || row.totalMoney === "") {
        this.$message({
          showClose: true,
          message: "价税合计必须是正数,且小数点后最多两位",
          type: "warning",
        });
        // 将字符串转换为浮点数
        let discountValue = parseFloat(row.totalMoney);

        // 如果isNaN，则设置为0
        if (isNaN(discountValue)) {
          row.totalMoney = 0;
        } else {
          if (discountValue < 0) {
            row.totalMoney = 0;
          } else {
            // 四舍五入到两位小数
            row.totalMoney = parseFloat(discountValue.toFixed(2));
          }
        }
      }

      row.purchasePrice = (
        row.totalMoney /
        (1 + row.taxRate / 100) /
        row.totalNum /
        (1 - row.discount / 100)
      ).toFixed(2);

      this.calculateRow(row);
    },
    calculateRowTotalPrice(row) {
      //含税单价
      // 使用正则表达式验证输入值是否为有效的正整数
      const positiveNumberRegex = /^\d+(\.\d{1,2})?$/; // 匹配正数，小数点最多两位
      // 验证和重置价税合计（假设价税合计可以是浮点数）
      if (!positiveNumberRegex.test(row.totalPrice) || row.totalPrice === "") {
        this.$message({
          showClose: true,
          message: "含税单价必须是正数,且小数点后最多两位",
          type: "warning",
        });
        // 将字符串转换为浮点数
        let discountValue = parseFloat(row.totalPrice);

        // 如果isNaN，则设置为0
        if (isNaN(discountValue)) {
          row.totalPrice = 0;
        } else {
          if (discountValue < 0) {
            row.totalPrice = 0;
          } else {
            // 四舍五入到两位小数
            row.totalPrice = parseFloat(discountValue.toFixed(2));
          }
        }
      }

      row.purchasePrice = (row.totalPrice / (1 + row.taxRate / 100)).toFixed(2);

      this.calculateRow(row);
    },
    calculateRowDiscountAmount(row) {
      if (row.purchasePrice == 0) {
        this.$message({
          showClose: true,
          message: "输入折扣额前，请先输入正确的购货单价（购货单价不可以为0）",
          type: "warning",
        });
        row.discountAmount = 0;
        return;
      }

      //折扣额
      // 使用正则表达式验证输入值是否为有效的正整数
      const positiveNumberRegex = /^\d+(\.\d{1,2})?$/; // 匹配正数，小数点最多两位
      // 验证和重置价税合计（假设价税合计可以是浮点数）
      if (
        !positiveNumberRegex.test(row.discountAmount) ||
        row.discountAmount === ""
      ) {
        this.$message({
          showClose: true,
          message: "含税单价必须是正数,且小数点后最多两位",
          type: "warning",
        });
        // 将字符串转换为浮点数
        let discountValue = parseFloat(row.discountAmount);

        // 如果isNaN，则设置为0
        if (isNaN(discountValue)) {
          row.discountAmount = 0;
        } else {
          if (discountValue < 0) {
            row.discountAmount = 0;
          } else {
            // 四舍五入到两位小数
            row.discountAmount = parseFloat(discountValue.toFixed(2));
          }
        }
      }

      row.discount = (
        (row.discountAmount / row.totalNum / row.purchasePrice) *
        100
      ).toFixed(2);

      this.calculateRow(row);
    },
    calculateRowAmount(row) {
      //金额
      //使用正则表达式验证输入值是否为有效的正整数
      const positiveNumberRegex = /^\d+(\.\d{1,2})?$/; // 匹配正数，小数点最多两位
      //验证和重置金额（假设价税合计可以是浮点数）
      if (!positiveNumberRegex.test(row.amount) || row.amount === "") {
        this.$message({
          showClose: true,
          message: "金额必须是正数,且小数点后最多两位",
          type: "warning",
        });
        // 将字符串转换为浮点数
        let discountValue = parseFloat(row.amount);

        // 如果isNaN，则设置为0
        if (isNaN(discountValue)) {
          row.amount = 0;
        } else {
          if (discountValue < 0) {
            row.amount = 0;
          } else {
            // 四舍五入到两位小数
            row.amount = parseFloat(discountValue.toFixed(2));
          }
        }
      }

      row.purchasePrice = (
        row.amount /
        row.totalNum /
        (1 - row.discount / 100)
      ).toFixed(2);

      this.calculateRow(row);
    },
    calculateRowTax(row) {
      //税额

      if (row.purchasePrice == 0) {
        this.$message({
          showClose: true,
          message: "输入税额前，请先输入正确的购货单价（购货单价不可以为0）",
          type: "warning",
        });
        row.discountAmount = 0;
        return;
      }

      //使用正则表达式验证输入值是否为有效的正整数
      const positiveNumberRegex = /^\d+(\.\d{1,2})?$/; // 匹配正数，小数点最多两位
      //验证和重置金额（假设价税合计可以是浮点数）
      if (!positiveNumberRegex.test(row.tax) || row.tax === "") {
        this.$message({
          showClose: true,
          message: "税额必须是正数,且小数点后最多两位",
          type: "warning",
        });
        // 将字符串转换为浮点数
        let discountValue = parseFloat(row.tax);

        // 如果isNaN，则设置为0
        if (isNaN(discountValue)) {
          row.tax = 0;
        } else {
          if (discountValue < 0) {
            row.tax = 0;
          } else {
            // 四舍五入到两位小数
            row.tax = parseFloat(discountValue.toFixed(2));
          }
        }
      }

      row.taxRate =
        (row.tax /
          row.totalNum /
          row.purchasePrice /
          (1 - row.discount / 100)) *
        100;

      this.calculateRow(row);
    },
  }
};